package it.omnisong.client.widget;

import it.omnisong.client.model.StockArtist;
import it.omnisong.client.resources.Resources;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

class GridWikilink extends LayoutContainer { 
	
	private Grid<StockArtist> grid = null;
	  
    private ColumnModel cm = null;  
	
	public GridWikilink() {
		
	}
	  
	protected void onRender(Element parent, int index) {  
		
		super.onRender(parent, index);  
	    setLayout(new FlowLayout(10));  
	  
	}  
	

	/* metodo che setta la grid per accogliere wikilinks */
	public void setWikilinkResult(List<StockArtist> retrieved) {
		
		if(retrieved!=null) {
		  
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
	  
	    ColumnConfig column = new ColumnConfig();  
	    column.setId("name");  
	    column.setHeader("Artist Name");  
	    column.setWidth(100);  
	    configs.add(column);  
	  
	    column = new ColumnConfig();  
	    column.setId("url");  
	    column.setHeader("Wikipedia Link");  
	    column.setWidth(250);  
	    configs.add(column);  
	    
	    this.cm = new ColumnModel(configs);
	    
		ListStore<StockArtist> store = new ListStore<StockArtist>();  
	    store.add(retrieved); 
		
		ContentPanel cp = new ContentPanel();
	    
	    cp.setId("cp");
	    cp.setBodyBorder(false);  
	    cp.setIcon(AbstractImagePrototype.create(Resources.ICONS.table()));  
	    cp.setHeading("Omnisong found this artists:");  
	    cp.setButtonAlign(HorizontalAlignment.CENTER);  
	    cp.setLayout(new FitLayout());  
	    //cp.setSize(300, 300);  
	  
	    this.grid = new Grid<StockArtist>(store, cm);  
	    this.grid.setAutoHeight(true);
	    this.grid.setAutoWidth(true);
	    this.grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	    this.grid.setStyleAttribute("borderTop", "none");  
	    this.grid.setAutoExpandColumn("url");  
	    this.grid.setBorders(true);  
	    this.grid.setStripeRows(true);  
	    cp.add(this.grid);
	    
	    this.add(cp); }
		else
			System.out.println("Lista vuota");
	}

}
